package com.anbinh.doctor.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.IRcallBak;
import com.anbinh.doctor.utils.Utils;

public class FragmentRegister extends Fragment {

	private View mView;
	private static FragmentRegister fragment = null;
	private IRcallBak callIResult;

	private EditText edPhone;
	private EditText edFullName;

	private int pos;
	private ProgressBar mLoadding;
	private RadioGroup radioGroup1;

	private TextView lblDangnhap;
	private String mPhone;
	private String mFullName;
	private String type = "doctor";

	public static FragmentRegister newInstance(Bundle data) {
		if (fragment == null)
			fragment = new FragmentRegister();
		fragment.setArguments(data);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof IRcallBak) {
			callIResult = (IRcallBak) activity;
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.frangment_register, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		Utils.setOnTouchHideKeyboard(getActivity(), (ViewGroup) mView);
		radioGroup1 = (RadioGroup) mView.findViewById(R.id.radioGroup1);
		radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				pos = radioGroup1.indexOfChild(mView.findViewById(checkedId));
				if (pos == 0) {
					type="doctor";
				}else{
					type="clinic";
				}
			}
		});

		edPhone = (EditText) mView.findViewById(R.id.edPhone);
		edFullName = (EditText) mView.findViewById(R.id.edFullName);

		mLoadding = (ProgressBar) mView.findViewById(R.id.loadding);
		lblDangnhap = (TextView) mView.findViewById(R.id.lblDangnhap);

		Button btnDangky = (Button) mView.findViewById(R.id.btnDangky);
		btnDangky.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mFullName = edFullName.getText().toString().trim();
				if (mFullName.isEmpty()) {
					Utils.showMessage(getActivity(),
							getResources().getString(R.string.nhap_ho_ten));
					return;
				}
				mFullName = mFullName.replace(" ", "%20");

				mPhone = edPhone.getText().toString().trim();
				if (mPhone.isEmpty()) {
					Utils.showMessage(
							getActivity(),
							getResources().getString(
									R.string.vui_long_nhap_so_dt));
					return;
				}

				if (mPhone.length() < 10 || mPhone.length() > 11) {
					Utils.showMessage(
							getActivity(),
							getResources().getString(
									R.string.so_dien_thoai_ko_hop_le));
					return;
				}
				mLoadding.setVisibility(View.VISIBLE);
				Utils.disableEnableControls(false, mView);

				ServerController.getInstance(getActivity()).requestRegister(
						mPhone, mFullName,type, new OnRequest<DataResponse>() {

							@Override
							public void onSuccess(DataResponse data) {
								mLoadding.setVisibility(View.GONE);
								Utils.disableEnableControls(true, mView);

								Utils.showMessage(getActivity(), String.format(
										getActivity().getResources().getString(
												R.string.ma_xa_thuc_da_dc_gui),
										mPhone));

								Bundle bundle = new Bundle();
								bundle.putInt(Utils.KEY, Utils.CREATE_ACC);
								bundle.putString(Utils.PHONE, mPhone);

								getActivity()
										.getSupportFragmentManager()
										.beginTransaction()
										.replace(
												R.id.fragment_container,
												FragmentActiveAccount
														.newInstance(bundle),
												"TAG_REGISTER")
										.addToBackStack(null).commit();

							}

							@Override
							public void onError(int errorCode, String message) {
								Utils.showMessage(getActivity(), message);
								mLoadding.setVisibility(View.GONE);
								Utils.disableEnableControls(true, mView);
							}
						});

			}
		});

		lblDangnhap.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getActivity().onBackPressed();
			}
		});

	}

}
